package com.chezhujie.www.controller;

import com.chezhujie.www.domain.Czjcomments;
import com.chezhujie.www.service.CzjcommentsPicService;
import com.chezhujie.www.service.CzjcommentsService;
import com.chezhujie.www.service.CzjcommentsVideoService;
import com.sys.core.page.Pagination;
import com.sys.core.utils.ConvertObj;
import com.sys.core.utils.UUIDUtils;
import com.sys.user.base.controller.BaseController;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *   评论
 * Created by 汤伟杰 on 2017/5/23.
 */
@Controller
@RequestMapping("/czjcomments")
public class CzjcommentsController extends BaseController<Czjcomments> {
    @Autowired
    private CzjcommentsService czjcommentsService;
    @Autowired
    private CzjcommentsPicService czjcommentsPicService;
    @Autowired
    private CzjcommentsVideoService czjcommentsVideoService;

    /** 列表 **/
    @RequestMapping("/list.do")
    public String list(Integer pageNo, Integer pageSize, Czjcomments czjcomments, HttpServletRequest req, HttpServletResponse resp,
                       ModelMap model){
        setMsg(czjcomments,req,model);
        setUserinfoId(model);
        if (pageNo == null) {
            pageNo = 1;
        }
        if (pageSize == null) {
            pageSize = 0;
        }
        try {
            Map<String, Object> params = ConvertObj.toMap(czjcomments);
            Pagination pagination = czjcommentsService.list(pageNo.intValue(), pageSize.intValue(), params, model);
            model.addAttribute("pagination", pagination);
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("failed","获取评论列表失败!"+e.getMessage());
        }
        model.addAttribute("entity", czjcomments);
        return "czjcomments/list";
    }
    /**
    * api接口评论列表
    * @param pageNo
    * @param pageSize
    * @param czjcomments
    * @param req
    * @param resp
    * @param model
    * @throws Exception
    */
    @RequestMapping("/apiList.jn")
    public void apiList(@RequestParam(required = false)String userinfoId,
                        Integer joinlpk,//是否参加乐拍客
                        @RequestParam(required = true)String objectId,Integer pageNo, Integer pageSize, Czjcomments czjcomments,
                        HttpServletRequest req, HttpServletResponse resp,
                        ModelMap model) throws Exception {
        if (pageNo == null) {
            pageNo = 1;
        }
        if (pageSize == null) {
            pageSize = 0;
        }
        try {
            Map<String, Object> map = new HashMap<String, Object>();
            if(joinlpk!=null&&joinlpk==1){
                map.put("joinlpk", 1);
            }

            map.put("objectId", objectId);
            map.put("userinfoId", userinfoId);
            Pagination pagination = czjcommentsService.apilist(pageNo.intValue(), pageSize.intValue(), map, model);
            jsonResult(1,"","",pagination,resp);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult(0,"","获取评论列表失败!"+e,"",resp);
        }

    }
    @RequestMapping("/apiLpkList.jn")
    public void apiLpkList(Integer pageNo, Integer pageSize, Czjcomments czjcomments,
                        HttpServletRequest req, HttpServletResponse resp,
                        ModelMap model) throws Exception {
        if (pageNo == null) {
            pageNo = 1;
        }
        if (pageSize == null) {
            pageSize = 0;
        }
        try {
            Map<String, Object> map = new HashMap<String, Object>();

            Pagination pagination = czjcommentsService.apilpklist(pageNo.intValue(), pageSize.intValue(), map, model);
            jsonResult(1,"","",pagination,resp);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult(0,"99","获取乐拍客评论列表失败!"+e,"",resp);
        }

    }
    /**
    * api接口评论详情
    * @param req
    * @param resp
    * @param model
    * @throws Exception
    */
    @RequestMapping("/apiDetail.jn")
    public void apiDetail(HttpServletRequest req, HttpServletResponse resp,
    ModelMap model) throws Exception {
        Map<String, Object> params = new HashMap<String, Object>();
        Enumeration<String> enumeration = req.getParameterNames();
        if(enumeration.hasMoreElements()){
            String next = enumeration.nextElement();
            params.put(next, req.getParameter(next));
        }

        try {
            Map<String,Object> result = czjcommentsService.getApiDetail(params);
            jsonResult(1,"","",result,resp);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult(0,"99","获取评论详情失败!"+e,"",resp);
        }

    }
    /** api测试页面 **/
    @RequestMapping("/apiTest.do")
    public String apiList(HttpServletRequest req, HttpServletResponse resp,
        ModelMap model) throws Exception {
        model.addAttribute("entity",new HashMap<String,Object>());
        return "czjcomments/apiTest";

    }

    /** 保存 **/
    @RequestMapping("/save.do")
    public String save(Czjcomments czjcomments,
                      HttpServletRequest req, HttpServletResponse resp,
                       ModelMap model){
        try {

            if(StringUtils.isNotBlank(czjcomments.getCommentsId())){
                czjcommentsService.update(czjcomments);


            }else{
                czjcomments.setCommentsId(UUIDUtils.getUUID());
                czjcommentsService.insert(czjcomments);
            }
            model.addAttribute("success", "保存评论成功");
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("failed", "保存评论失败!详情："+e.getMessage());
        }
        return "redirect:list.do";
    }


    /** 详情 **/
    @RequestMapping("/detail.do")
    public String detail(@RequestParam(required = true)String commentsId,Czjcomments czjcomments, HttpServletRequest req, HttpServletResponse resp,
                       ModelMap model){
        setMsg(czjcomments,req,model);
        setUserinfoId(model);
        try {
            Czjcomments entity = czjcommentsService.queryById(czjcomments.getCommentsId());
            model.addAttribute("entity", entity);
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("failed", "查询评论出错!详情："+e.getMessage());
        }
        return "czjcomments/detail";
    }
    /** 新详情 **/
    @RequestMapping("/detail2.do")
    public String detail2(@RequestParam(required = true)String commentsId,Czjcomments czjcomments, HttpServletRequest req, HttpServletResponse resp,
                         ModelMap model){
        setMsg(czjcomments,req,model);
        setUserinfoId(model);
        try {
            Czjcomments entity = czjcommentsService.queryById(czjcomments.getCommentsId());
            List<Map<String,Object>> listPic = czjcommentsPicService.getByCommentsId(entity.getCommentsId());
            List<Map<String,Object>> listVid = czjcommentsVideoService.getByCommentsId(entity.getCommentsId());

            Map<String, Object> map = ConvertObj.toMap(entity);
            map.put("listPic",listPic);
            map.put("listVid",listVid);
            model.addAttribute("entity", map);
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("failed", "查询评论出错!详情："+e.getMessage());
        }
        return "czjcomments/detail2";
    }
    /** 新增或修改 **/
    @RequestMapping("/add.do")
    public String add(Czjcomments czjcomments, HttpServletRequest req, HttpServletResponse resp,
                       ModelMap model){
        setMsg(czjcomments,req,model);
        setUserinfoId(model);
        if(StringUtils.isNotBlank(czjcomments.getCommentsId())){
            czjcomments = czjcommentsService.queryById(czjcomments.getCommentsId());
            model.addAttribute("entity", czjcomments);
        }
        return "czjcomments/add";
    }
    /** 删除 **/
    @RequestMapping("/delete.do")
    public String delete(@RequestParam(required = true)String commentsId,Czjcomments czjcomments, HttpServletRequest req, HttpServletResponse resp,
                     ModelMap model){
        try {
            czjcommentsService.deleteById(czjcomments.getCommentsId());
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("failed", e.getMessage());
        }
        return "redirect:list.do";
    }
    private void setUserinfoId(ModelMap model) {
        Map<String,Object> param = new HashMap<String,Object>();
        List<Map<String,Object>> listUserinfoId = czjcommentsService.getListUserinfoId(param);
        // map.put("name","");
        // map.put("value","");
        model.addAttribute("listUserinfoId", listUserinfoId);
    }
    /** 审批 **/
    @RequestMapping("/approve.do")
    public String approve(@RequestParam(required = true)String commentsId,
                          @RequestParam(required = true)Integer approveStatus,
                          HttpServletRequest req, HttpServletResponse resp,
                          ModelMap model){

        czjcommentsService.changeApprove(commentsId,approveStatus);
        return "redirect:detail.do?commentsId="+commentsId;
    }
    /** 审批包含图片和视频 **/
    @RequestMapping("/approve2.do")
    public String approve2(@RequestParam(required = true)String commentsId,
                          @RequestParam(required = true)Integer approveStatus,
                          HttpServletRequest req, HttpServletResponse resp,
                          ModelMap model){

        czjcommentsService.changeApprove2(commentsId,approveStatus);
        return "redirect:detail2.do?commentsId="+commentsId;
    }
    private void setMsg(Czjcomments czjcomments,HttpServletRequest req, ModelMap model) {
        model.addAttribute("failed", req.getParameter("failed"));
        model.addAttribute("success", req.getParameter("success"));
        model.addAttribute("entity", czjcomments);
    }
}
